package com.lzc.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.lzc.pojo.Client;
import com.lzc.service.ClientService;
import com.lzc.service.impl.ClientServiceImpl;

/**
 * Servlet implementation class ClientServlet
 */
@WebServlet("/client")
public class ClientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(ClientService.class);
	//获取service层对象
	ClientService cs = new ClientServiceImpl();
	
	@Override
		protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			//设置请求编码格式
			req.setCharacterEncoding("utf-8");
				//设置响应编码格式
			resp.setContentType("text/html;charset=utf-8");
			//获取操作符
			String oper=req.getParameter("oper");
			if ("up".equals(oper)) {
				ClientUpload(req,resp);
			} else if ("show".equals(oper)) {
				ClientShow(req,resp);
			} else {
				logger.debug("没有找到对应的操作符"+oper);
			}
			
			
		}
	//列表展示客户信息
	private void ClientShow(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		String page = req.getParameter("page");
		List<Client> lc = cs.clientShowService(page);
		
		
	}
	//添加客户信息
	private void ClientUpload(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		String uname = req.getParameter("uname");
		String phone = req.getParameter("phone");
		String email = req.getParameter("email");
		String sex = req.getParameter("sex");
		String birth = req.getParameter("birth");
		String[] bs=null;
			if (birth!="") {
			bs = birth.split("/");
			birth=bs[2]+"-"+bs[0]+"-"+bs[1];
		}
		String address = req.getParameter("address");
		int flag = 0;
		System.out.println(uname+":"+phone+":"+email+":"+sex+":"+birth+":"+address);
		
		Client c = new Client(0, uname, phone, email, sex, birth, address, flag);
		int index = cs.ClientUploadService(c);
	}
    

}
